<template>
  <!-- 容器 -->
  <el-container class="container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 主体区 -->
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 1.菜单组件 -->
        <!-- 菜单折叠按钮 -->
        <div class="toggle-button" @click="toggleCollapse">
          <div
            :class="isCollapse ? 'el-icon-caret-right' : 'el-icon-caret-left'"
          ></div>
          <div
            :class="isCollapse ? 'el-icon-caret-right' : 'el-icon-caret-left'"
          ></div>
          <div
            :class="isCollapse ? 'el-icon-caret-right' : 'el-icon-caret-left'"
          ></div>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="isActivePath"
        >
          <!-- 2.子菜单：容器 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 子菜单 文字模板 -->
            <template slot="title">
              <i :class="item.iconclass"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 3.菜单项：可以用来点击 导航的-->
            <el-menu-item
              :index="'/' + items.path"
              v-for="items in item.children"
              :key="items.id"
              @click="saveDefaultMenu('/' + items.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ items.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'app',
  data: function () {
    return {
      // 菜单数据（通过异步请求服务接口获得）
      menulist: [],
      isCollapse: false,
      isActivePath: '/users'
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出函数
    logout () {
      // 清空sessionStorage的数据
      window.sessionStorage.clear()
      // 跳转到登陆页面
      this.$router.push('login')
    },
    // 钩子函数 发送异步请求，获取所有的菜单
    async getMenuList () {
      // 请求 左侧菜单权限
      const { data: resBody } = await this.$http.get('menus')
      // 如果失败了，显示错误信息，并退出函数执行
      if (resBody.meta.status !== 200) return this.$message.error(resBody.meta.msg)
      this.menulist = resBody.data

      // 读取sessionStorage里面的defaultMenu值，设置默认高亮选中菜单项
      this.isActivePath = window.sessionStorage.getItem('defaultMenu')
    },
    // 菜单折叠
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveDefaultMenu (isActivePath) {
      window.sessionStorage.setItem('defaultMenu', isActivePath)
      this.isActivePath = isActivePath
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  user-select: none;
}
.el-header {
  background-color: #363c42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: -0.5em;
  cursor: pointer;
}
</style>
